<?php

namespace app\models;

use yii\web\IdentityInterface;
use Yii;

/**
 * This is the model class for table "{{%ns_user}}".
 *
 * @property integer $user_id
 * @property string $user_name
 * @property string $password
 * @property string $register_date
 * @property string $last_login_date
 * @property string $last_login_ip
 * @property integer $role
 * @property integer $status
 * @property string $expired_date
 * @property string $session_id
 * @property string $nick_name
 * @property string $url
 * @property string $email
 */
class User extends \yii\db\ActiveRecord implements IdentityInterface
{

    const ROLE_ADMIN = 2;
    const ROLE_TEACHER = 1;
    const ROLE_STUDENT = 0;

    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 1;
 
    const LOGIN_ACTIVE = 1;
    const LOGIN_DEACTIVE = 0;

    public function checkAccess($role)
    {
        return $this->role == $role ? true : false;
    }
    public function getRole()
    {
        if($this->role == self::ROLE_ADMIN)
        {
            return yii::t('User','Admin');
        }
        elseif($this->role == self::ROLE_TEACHER)
        {
            return yii::t('User','Teacher');
        }
        else
        {
            return yii::t('User','Student');
        }
    }
    public $repeatpassword;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%tbl_user}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['user_name', 'match', 'pattern'=>'/^[\w\d]+$/',
				'message'=>yii::t('User','Only characters and digits should used in user name')],
            [['user_name', 'password', 'repeatpassword','email'], 'required'],
            [['register_date', 'last_login_date', 'expired_date'], 'safe'],
            [['user_name'], 'string', 'max' => 26],
            [['nick_name'], 'string', 'min' => 4],
            [['user_name','email'], 'unique'],
            [['email'], 'email'],
            [['user_name'], 'string', 'min' => 6],
            [['last_login_ip', 'nick_name', 'url'], 'string', 'max' => 20],
            [['session_id'], 'string', 'max' => 50],
            ['repeatpassword', 'compare','compareAttribute'=>'password'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'user_id' => Yii::t('User', 'User ID'),
            'user_name' => Yii::t('User', 'User Name'),
            'password' => Yii::t('User', 'Password'),
            'register_date' => Yii::t('User', 'Register Date'),
            'last_login_date' => Yii::t('User', 'Last Login Date'),
            'last_login_ip' => Yii::t('User', 'Last Login Ip'),
            'role' => Yii::t('User', 'Role'),
            'status' => Yii::t('User', 'Status'),
            'expired_date' => Yii::t('User', 'Expired Date'),
            'session_id' => Yii::t('User', 'Session ID'),
            'nick_name' => Yii::t('User', 'Nick Name'),
            'url' => Yii::t('User', 'Url'),
            'department' => Yii::t('User', 'Department'),
            'professional' => Yii::t('User', 'Professional'),
            'class' => Yii::t('User', 'Class'),
            'email' => Yii::t('User', 'Email Address'),
            'repeatpassword' => Yii::t('User', 'Repeat Password'),
            'tel' => Yii::t('User', 'Tel'),
            'grade' => Yii::t('User', 'Grade'),
        ];
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne(['user_id' => $id, 'status' => self::STATUS_ACTIVE]);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }
    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return true;
    }



    /**
     * @TODO  : 验证用户密码
     * @param : string $username
     * @return: boolean
     * @author: Ray
     * @date  : 2014-09-26
     */
    public function validatePassword($password)
    {
        return Yii::$app->security->validatePassword($password, $this->password);
    }

    /**
     * @TODO  : 设置用户密码
     * @param : string $password
     * @return:
     * @author: Ray
     * @date  : 2014-09-26
     */
    public function setPassword($password)
    {
        $this->password = Yii::$app->security->generatePasswordHash($password);
    }

    /**
     * @TODO  : 按照用户名查找相应的用户模型
     * @param : string $username
     * @return: UserModel
     * @author: Ray
     * @date  : 2014-09-26
     */
    public static function findByUsername($username)
    {
        return User::find()->where(['user_name' => $username])->one();
    }

    public static function getUserNameListForMessage($grade, $department, $professional, $classes)
    {
        $result = NULL;
        if(empty($grade))
        {
            return $result;
        }
        elseif(empty($department))
        {
            $result = User::find()->where(['grade'=>$grade, 'status'=>1])->all();
        }
        elseif(empty($professional))
        {
            $result = User::find()->where(['grade'=>$grade, 'department'=>$department, 'status'=>1])->all();
        }
        elseif(empty($classes))
        {
            $result = User::find()->where(['grade'=>$grade, 'department'=>$department, 'professional'=>$professional, 'status'=>1])->all();
        }
        else
        {
            $result = User::find()->where(['grade'=>$grade, 'department'=>$department, 'professional'=>$professional, 'class'=>$classes, 'status'=>1])->all();
        }
        if($result !== NULL)
        {
            foreach($result as $elem)
            {
                $res[] = $elem->user_name;
            }
        }
        return $res;
    }
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGrade0()
    {
        return $this->hasOne(Grade::className(), ['grade_id' => 'grade']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getDepartment0()
    {
        return $this->hasOne(Department::className(), ['department_id' => 'department']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProfessional0()
    {
        return $this->hasOne(Professional::className(), ['professional_id' => 'professional']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getClass0()
    {
        return $this->hasOne(Classes::className(), ['classes_id' => 'class']);
    }
}
